package com.yjha.lzmarathonsys.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {

    /************************************ 点赞mq配置 ***************************************/
    public static final String QUEUE_LIKE = "likeQueue";
    public static final String EXCHANGE_LIKE = "likeExchange";
    public static final String ROUTING_KEY_LIKE = "like.key";
    /**
     * 点赞队列（持久化）
     */
    @Bean(QUEUE_LIKE)
    public Queue likeQueue() {
        return QueueBuilder.durable(QUEUE_LIKE).build(); // 持久化队列
    }

    /**
     * 点赞直连交换机（持久化）
     */
    @Bean(EXCHANGE_LIKE)
    public DirectExchange likeExchange() {
        return ExchangeBuilder.directExchange(EXCHANGE_LIKE)
                .durable(true) // 持久化交换机
                .build();
    }

    /**
     * 绑定点赞队列到交换机
     */
    @Bean
    public Binding bindingLikeQueue(
            @Qualifier(QUEUE_LIKE) Queue queue,
            @Qualifier(EXCHANGE_LIKE) DirectExchange exchange) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with(ROUTING_KEY_LIKE);
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /************************************ 评论配置 ***************************************/
    public static final String QUEUE_COMMENT= "commentQueue";
    public static final String EXCHANGE_COMMENT = "commentExchange";
    public static final String ROUTING_KEY_COMMENT = "comment.key";

    /**
     * 评论队列（持久化）
     */
    @Bean(QUEUE_COMMENT)
    public Queue commentQueue() {
        return QueueBuilder.durable(QUEUE_COMMENT).build(); // 持久化队列
    }

    /**
     * 评论直连交换机（持久化）
     */
    @Bean(EXCHANGE_COMMENT)
    public DirectExchange commentExchange() {
        return ExchangeBuilder.directExchange(EXCHANGE_COMMENT)
                .durable(true) // 持久化交换机
                .build();
    }

    /**
     * 绑定评论队列到交换机
     */
    @Bean
    public Binding bindingCommentQueue(
            @Qualifier(QUEUE_COMMENT) Queue queue,
            @Qualifier(EXCHANGE_COMMENT) DirectExchange exchange) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with(ROUTING_KEY_COMMENT);
    }
}
